package com.cskaoyan.demo.service;

import com.cskaoyan.demo.bean.MarketUser;
import com.cskaoyan.demo.bean.MarketUserExample;
import com.cskaoyan.demo.bean.admin.userInfo;
import com.cskaoyan.demo.bean.common.CommonData;
import com.cskaoyan.demo.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author stone
 * @date 2023/03/17 10:32
 */

@Transactional
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public MarketUser query(String username, String password) {

        return query(username, password, null);
    }

    @Override
    public MarketUser query(String username, String password, String avatar) {

        /*String passwordHash=null;

        try {
            // 创建MessageDigest对象并指定算法为SHA-256
            MessageDigest sha256Digest = MessageDigest.getInstance("SHA-256");

            // 将字符串转换为字节数组
            byte[] inputBytes = password.getBytes("UTF-8");

            // 更新MessageDigest对象的哈希值
            sha256Digest.update(inputBytes);

            // 计算哈希值
            byte[] hashBytes = sha256Digest.digest();

            // 将字节数组转换为十六进制字符串
            StringBuilder hexStringBuilder = new StringBuilder();
            for (byte b : hashBytes) {
                hexStringBuilder.append(String.format("%02x", b));
            }

            passwordHash = hexStringBuilder.toString();

        } catch (NoSuchAlgorithmException | java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
*/
        /*String salt = BCrypt.gensalt(10);

        // 使用BCrypt加密密码
        String hashedPassword = BCrypt.hashpw(password, salt);*/


        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        criteria.andUsernameEqualTo(username);
        if (avatar != null && avatar.length() > 0) {
            criteria.andAvatarEqualTo(avatar);
        }

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);

        if (marketUsers != null && !marketUsers.isEmpty()) {
            for (MarketUser user : marketUsers) {
                if (BCrypt.checkpw(password, user.getPassword())) {
                    return user;
                }
            }
        }

        return null;

    }

    @Override
    public CommonData query(userInfo userInfo) {

        PageHelper.startPage(userInfo.getPage(), userInfo.getLimit());

        String orderByClause = userInfo.getSort() + " " + userInfo.getOrder();
        MarketUserExample example=new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause(orderByClause);

        if (userInfo.getUsername() != null && !"".equals(userInfo.getUsername())) {
            criteria.andUsernameLike("%" + userInfo.getUsername() + "%");
        }


        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);

        CommonData data = CommonData.data(marketUsers);

        return data;
    }

    @Override
    public CommonData list(userInfo userInfo) {
        return null;
    }
}
